package com.zerosky.crm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.ContractDAO;
import com.zerosky.crm.po.ChartSales;
import com.zerosky.crm.po.Contract;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.service.ChartService;
import com.zerosky.crm.service.ContractService;
import com.zerosky.crm.util.PageInfo;

/**
 * Contract业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="contractService")
public class ContractServiceImpl implements ContractService {

	@Autowired
	private ContractDAO contractDAO; // Contract数据接口

	@Autowired
	private ChartService chartService;

	/**
	 * Contract列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<Contract> getContractList(Map map) {
		return contractDAO.getContractList(map);
	}

	/**
	 * 分页查询信息
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Contract> getPageInfoList(Map map, PageInfo<Contract> page) throws Exception {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		if ("1".equals(map.get("isCheck"))) {
			List<Contract> list = contractDAO.getContractCheckList(map);
			page.setList(list);
			int count = contractDAO.countCheckContract(map);// 获取总记录
			page.setPageCount(count);
		} else {
			List<Contract> list = contractDAO.getContractList(map);
			page.setList(list);
			int count = contractDAO.countContract(map);// 获取总记录
			page.setPageCount(count);
		}
		return page;
	}

	/**
	 * 通过主键查找Contract实体
	 * 
	 * @param id
	 *            Contract主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Contract getContractById(String id) {
		return contractDAO.getContractById(id);
	}

	/**
	 * 保存Contract
	 * 
	 * @param Contract
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveContract(Contract contract, String operation, Employee user) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
			contractDAO.addContract(contract);
		} else if ("修改".equalsIgnoreCase(operation)) {
			// 如果合同状态为3（审核不通过）或4（制单生成）就会增加审核人id，审核人名称，审核时间
			if (contract.getContractStatus() == 3 || contract.getContractStatus() == 4) {
				contract.setCheckPeopleId(user.getEmployeeId());
				contract.setCheckPeopleName(user.getName());
				contract.setCheckTime(new Date());
			}
			contractDAO.updateContract(contract);
		}
		if (contract.getContractStatus() == 4) {// 判断是否制单形成。如已制单形成
			ChartSales chartSales = new ChartSales();
			chartSales.setId(UUID.randomUUID().toString());
			chartSales.setEmpName(user.getName());
			chartSales.setDeptId(user.getDept().getDeptId());
			if (user.getBranch() != null) {
				chartSales.setBranchId(user.getBranch().getBranchId());
			}
			if (user.getTeam() != null) {
				chartSales.setTeamId(user.getTeam().getBranchId());
			}

			chartSales.setStartDate(new Date());
			chartSales.setEmpId(user.getEmployeeId());
			chartSales.setSalesAmount(contract.getTransactionAmount());
			chartSales.setContractId(contract.getContractId());
			chartService.saveChatSales(chartSales);// 添加合同业绩
		}
	}

	/**
	 * 删除Contract
	 * 
	 * @param idArray
	 *            Contract主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteContractById(String id) throws Exception {
		contractDAO.deleteContractById(id);
	}

	@Override
	public Double getContractSum(String productName) { 
		return contractDAO.getContractSum(productName);
	}

}
